//
//  ProductDetailEvaluationHeader.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/21.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "StoreDetailImageShowCell.h"

#import "ProductDetailEvaluationHeader.h"

@interface ProductDetailEvaluationHeader ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>{
    CGSize _cellSize;
    UIEdgeInsets _collectionEdge;
}

@end
@implementation ProductDetailEvaluationHeader

+ (CGFloat)calculateHeightWithModel:(CommonEvaluateModel *)model {
    
    CGFloat userInfoHeight = 60.0f;
    CGFloat labelHeight = [Utilities getStringHeightWithText:model.ueEvaluateContent font:[UIFont systemFontOfSize:12] viewWidth:TTScreenWidth-30];

    NSInteger lineCount = model.imageUrls.count/3;
    if (model.imageUrls.count%3>0) {
        lineCount++;
    }
    CGFloat collectionHeight = lineCount * ((TTScreenWidth - 30)/3);
    return collectionHeight + labelHeight + userInfoHeight + 10 + 40;
}

- (void)fillHeadWithModel:(CommonEvaluateModel *)model {
    
    self.model = model;
    
    if (model.ueEvaluateStar > 0) {
        self.starLab.text = [NSString stringWithFormat:@"%.1f分",model.ueEvaluateStar];
    } else {
        self.starLab.text = @"暂无评分";
    }
    
    self.infoHeight.constant = model.infoHeight;
    self.evaluateTextHeight.constant = model.evaluateTextHeight;
    self.collectionViewHeight.constant = model.collectionViewHeight;
    self.thumbHeight.constant = model.thumbHeight;
    
    self.userName.text = model.nickname;
    self.evaluationTime.text = model.ueEvaluateTime;
    
    if (model.imgUrl.length > 0) {
        [self.userIcon sd_setImageWithURL:[NSURL URLWithString:model.imgUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    }
    
    self.evaluationContent.text = model.ueEvaluateContent;
    [self.collectionview reloadData];
}

- (void)awakeFromNib {
    [super awakeFromNib];

    CGFloat width = TTScreenWidth - 30;
    
    CGFloat cellWidth;
    CGFloat distance = 5;
    cellWidth = width/3 - 2*distance;

    _cellSize =  CGSizeMake(cellWidth, cellWidth);
    _collectionEdge = UIEdgeInsetsMake(distance, distance, distance, distance);
    
    UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc]init];
    flowLaout.scrollDirection = UICollectionViewScrollDirectionVertical;

    self.collectionview.collectionViewLayout = flowLaout;
    self.collectionview.delegate = self;
    self.collectionview.dataSource = self;
    self.collectionview.showsHorizontalScrollIndicator = NO;
    self.collectionview.showsVerticalScrollIndicator = NO;
    
    [self.collectionview registerNib:[UINib nibWithNibName:@"StoreDetailImageShowCell" bundle:nil] forCellWithReuseIdentifier:@"StoreDetailImageShowCellID"];

}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.userIcon pp_clipCorners:UIRectCornerAllCorners radius:self.userIcon.frame.size.width/2];


        [self.thumbBtn pp_clipCorners:UIRectCornerAllCorners radius:self.thumbBtn.frame.size.height/2 border:1 color:UIColorHex(0xf1f1f1)];
    });
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    StoreDetailImageShowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StoreDetailImageShowCellID" forIndexPath:indexPath];

    NSString *url = self.model.imageUrls[indexPath.row].url;
    
    [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (self.model.imageUrls.count > 9) {
        return 9;
    } else {
        return self.model.imageUrls.count;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return _cellSize;
}

//设置cell的位置
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                       layout:(UICollectionViewLayout *)collectionViewLayout
       insetForSectionAtIndex:(NSInteger)section {

    return _collectionEdge;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}

- (CommonEvaluateModel *)model {
    if (!_model) {
        _model = [[CommonEvaluateModel alloc] init];
    }
    return _model;
}

@end
